package com.example.wl.tiaoyinqi.yueqifragments;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.example.wl.tiaoyinqi.MyCallbacks;
import com.example.wl.tiaoyinqi.R;

/**
 * Created by wl on 2016/5/24.
 */
public class PipaFragment extends Fragment implements View.OnClickListener {
    private int lastSelectedXian;
    private ImageButton[] pipaXians;
    private MyCallbacks mCallback;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mCallback = (MyCallbacks) context;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallback = null;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_pipa, container, false);
        lastSelectedXian = 0;
        int[] pipaXianIds = new int[]{
                R.id.pipaXian_1,
                R.id.pipaXian_2,
                R.id.pipaXian_3,
                R.id.pipaXian_4
        };
        pipaXians = new ImageButton[pipaXianIds.length];
        for (int i = 0; i < pipaXianIds.length; i++) {
            pipaXians[i] = (ImageButton) v.findViewById(pipaXianIds[i]);
            pipaXians[i].setOnClickListener(this);
        }

        return v;
    }

    @Override
    public void onClick(View v) {
        for (int i = 0; i < pipaXians.length; i++) {
            if (pipaXians[i].getId() == v.getId()) {
                if (i != lastSelectedXian) {
                    if (lastSelectedXian != -1) {
                        pipaXians[lastSelectedXian].setImageResource(R.drawable.shuxian_normal);
                    }
                    pipaXians[i].setImageResource(R.drawable.shuxian_selected);
                    lastSelectedXian = i;
                }
                mCallback.onXianClick(i);
                break;
            }
        }
    }
}
